package com.itheima.controller;


import cn.hutool.core.bean.BeanUtil;
import com.itheima.domain.dto.LoginFormDTO;
import com.itheima.domain.dto.UserDTO;
import com.itheima.domain.entity.User;
import com.itheima.domain.entity.UserInfo;
import com.itheima.domain.vo.Result;
import com.itheima.service.IUserInfoService;
import com.itheima.service.IUserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 坤哥
 * @since 2025-10-06
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private IUserService userService;
    @Resource
    private IUserInfoService userInfoService;

    /**
     * 获取验证码
     */
    @PostMapping("/code")
    public Result getCheckCode(@RequestParam("phone") Long phone){
        log.info("登陆的电话号码为：{}",phone);
        return userService.getCheckCode(phone);
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result userLogin(@RequestBody LoginFormDTO loginFormDTO){
        log.info("用户登录信息：{}",loginFormDTO);
        return userService.userLogin(loginFormDTO);
    }

    /**
     * 查询个人主页
     */
    @GetMapping("/me")
    public Result getUserInfo(){
        log.info("查询个人主页:");
        return userService.getUserInfo();
    }

    /**
     * 查询博主信息
     */
    @GetMapping("/info/{id}")
    public Result info(@PathVariable("id") Long userId){
        // 查询详情
        UserInfo info = userInfoService.getById(userId);
        if (info == null) {
            // 没有详情，应该是第一次查看详情
            return Result.ok();
        }
        info.setCreateTime(null);
        info.setUpdateTime(null);
        // 返回
        return Result.ok(info);
    }
    /**
     * 查询博主主页
     */
    @GetMapping("/{id}")
    public Result queryUserById(@PathVariable("id") Long userId){
        // 查询详情
        User user = userService.getById(userId);
        if (user == null) {
            return Result.ok();
        }
        UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
        // 返回
        return Result.ok(userDTO);
    }
}
